<?php

namespace app;

use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\Label\Label;
use Endroid\QrCode\Logo\Logo;
use Endroid\QrCode\Writer\PngWriter;
use Endroid\QrCode\Writer\WebPWriter;

class Qrcode
{
    public function createQrcode($text, $size): \Endroid\QrCode\QrCode
    {
        return \Endroid\QrCode\QrCode::create($text)->setEncoding(new Encoding('UTF-8'))->setSize($size)->setMargin(10);
    }

    public function createLogo($path, $size): Logo
    {
        return Logo::create($path)->setResizeToWidth($size)->setResizeToHeight($size);
    }

    public function createLabel($text): Label
    {
        return Label::create($text);
    }

    public function output($type, $qrcode, $logo = null, $label = null)
    {
        $write = new WebPWriter();
        $file = $write->write($qrcode, $logo, $label);
        $file->saveToFile(public_path() . $type . "_qrcode.webp");
    }

    public function jpg($type,$name, $qrcode, $logo = null, $label = null)
    {
        $write = new PngWriter();
        $file = $write->write($qrcode, $logo, $label);
        $files = $file->getString();
        file_put_contents(public_path() . 'upload/qrcode/' . $type .'/'.$name. '.jpg',$files);
    }

    public function files($qrcode, $logo = null, $label = null)
    {
        $write = new WebPWriter();
        $file = $write->write($qrcode, $logo, $label);
        return $file->getString();
    }
}
